import { message } from 'antd';
import { getPackageOrderList, getPackageOrderDetail, updateIsReward, checkAndAssign, assignMaster, checkOrder } from '../services/api';

export default {
  namespace: 'packageOrder',

  state: {
    data: {
      list: [],
      pagination: {},
    },
    packageOrderInfo: {},
    packageOrderList: [],
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      const response = yield call(getPackageOrderList, payload);
      yield put({
        type: 'packageOrderLists',
        payload: response,
      });
    },
    *getPackageOrderDetail({ payload }, { call, put }) {
      const response = yield call(getPackageOrderDetail, payload);
      yield put({
        type: 'packageOrderInfos',
        payload: response,
      });
    },
    *updateIsReward({ payload, callback }, { call }) {
      const response = yield call(updateIsReward, payload);
      if (response.code === 0) {
        message.success('更改成功');
      } else {
        message.error('更改失败');
      }
      if (callback) callback();
    },
    *checkAndAssign({ payload, callback }, { call }) {
      const response = yield call(checkAndAssign, payload);
      if (response.code === 0) {
        message.success('更新成功');
        if (callback) callback();
      } else {
        message.error('更新失败');
      }
    },
    *assignMaster({ payload, callback }, { call, _ }) {
      const response = yield call(assignMaster, payload);
      if (response.code === 0) {
        if (callback) callback();
      } else {
        message.error('更新失败');
      }
    },
    *checkOrder({ payload, callback }, { call, _ }) {
      const response = yield call(checkOrder, payload);
      if (response.code === 0) {
        if (callback) callback();
      } else {
       
      }
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
    changeLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },

    packageOrderLists(state, action) {
      return {
        ...state,
        packageOrderList: action.payload.data,
      };
    },
    packageOrderInfos(state, action) {
      return {
        ...state,
        packageOrderInfo: action.payload.data || {},
      };
    },
  },
};
